package com.vf.admin.miaodong.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppComment;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 动态评论
 */
@Repository
public class AfterCommentDao extends BaseDao<AppComment> {

    /**
     * 查询列表
     */
    public List<AppComment> list(Map<String, Object> paraMap, SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_comment where 1=1  ");

        if (StringEx.isNotNull(paraMap.get("nick"))) {
            sbStr.append(" and nick = ? ");
            paramList.add(paraMap.get("nick"));
        }

        if (StringEx.isNotNull(paraMap.get("phone"))) {
            sbStr.append(" and phone = ? ");
            paramList.add(paraMap.get("phone"));
        }

        if (StringEx.isNotNull(paraMap.get("dynamicid"))) {
            sbStr.append(" and dynamicid = ? ");
            paramList.add(paraMap.get("dynamicid"));
        }
        if (!StringEx.isNull(paraMap.get("startDate"))) {
            sbStr.append(" and createtime>=? ");
            paramList.add(paraMap.get("startDate"));
        }
        if (!StringEx.isNull(paraMap.get("endDate"))) {
            sbStr.append(" and createtime<=? ");
            paramList.add(paraMap.get("endDate"));
        }
        if (!StringEx.isNull(paraMap.get("status"))) {
            sbStr.append(" and status=? ");
            paramList.add(paraMap.get("status"));
        }

        sbStr.append(" order by num desc");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppComment.class, splitPage);
    }

    /**
     * 详情
     */
    public AppComment get(Integer id) {
        if (StringEx.isNullOrLing(id)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_comment where 1=1");
        sbStr.append(" and id = ? ");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppComment.class);
    }

    /**
     * 添加/修改
     */
    public void saveOrUpdate(AppComment appComment) {
        if (appComment!=null || appComment.getId() != null && appComment.getId() > 0) {
            this.update(appComment);
        } else {
            appComment.setId(this.add(appComment));
        }
    }

    /**
     * 删除
     */
    public void del(Integer id) {
        if (id <= 0) {
            return;
        }
        String sql = "delete  from app_comment where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }


    /**
     * 更新状态
     */
    public void updateStatus(Integer id, Integer status) {
        if (id <= 0) {
            return;
        }
        String sql = "update app_comment set status = ?  where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(status);
        paramList.add(id);
        this.del(sql, paramList);
    }

    public void pinglunNumUpdate(Integer id, Integer status) {
        if (id==null ||id <= 0 || status==null || status<=0) {
            return;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuffer sql =new StringBuffer("update app_dynamic set");
        if(status==1){
            sql.append(" commentnum = commentnum+1 ");
        }else if(status==2){
            sql.append(" commentnum = commentnum-1 ");
        }
        sql.append(" where id = ?");
        paramList.add(id);
        this.update(sql.toString(), paramList);
    }

}
